#include <iostream>
using namespace std;
template <class T>
class List
{
private:
	T* m_list;
	int m_size;
	int m_capacity;
	int perMore = 10;
public:
	List()
	{
		m_capacity = perMore;
		m_list = new T[m_capacity];
		m_size = 0;
	}
	List(T data) : List()
	{
		m_list[m_size++] = data;
	}
	List(List& list)
	{
		m_capacity = list.m_size + perMore;
		m_size = list.m_size;
		list.m_list = new T[m_capacity];
		for (int i = 0; i < m_size; ++i)
		{
			m_list[i] = list.m_list[i];
		}
	}
	~List()
	{
		m_size = 0;
		m_capacity = 0;
		if (m_list == nullptr)
			return;
		delete[] m_list;
		m_list = nullptr;
	}
public: // 尾插尾删 头插头删 按下标插入 指定内容删除单个 指定内容删除所有 清空 
	void push_back(T data)
	{
		if (m_size == m_capacity)
			moreCapacity();
		m_list[m_size++] = data;
	}
	void pop_back()
	{
		if (empty())
			return;
		m_size--;
	}
	void push_front(T data)
	{
		if (m_size == m_capacity)
			moreCapacity();
		int current = m_size;
		while (current)
		{
			m_list[current] = m_list[current - 1];
			current--;
		}
		m_list[current] = data;
		m_size++;
	}
	void pop_front()
	{
		if (empty())
			return;
		int current = 1;
		while (current != m_size)
		{
			m_list[current - 1] = m_list[current];
			current++;
		}
		m_size--;
	}
	void insert(T data, int pos)
	{
		if (m_size == m_capacity)
			moreCapacity();
		int current = m_size;
		while (current != pos)
		{
			m_list[current] = m_list[current - 1];
			current--;
		}
		m_list[current] = data;
		m_size++;
	}
	void erase(T data) // 指定内容删除
	{
		int pos = find(data);
		if (pos == -1)
			return;
		while (pos + 1 != m_size)
		{
			m_list[pos] = m_list[pos + 1];
			pos++;
		}
		m_size--;
	}
	void eraseAll(T data) // 指定删除所有对应的内容
	{
		int fast = 0, slow = 0;
		while (fast != m_size)
		{
			if (m_list[slow] != data)
				slow++;
			m_list[slow] = m_list[++fast];
		}
		m_size = m_size - (fast - slow);
	}
	void clear() // 清空
	{
		delete[] m_list;
		m_list = nullptr; // 记得置为空指针，避免析构时重复释放
		m_capacity = 0;
		m_size = 0;
	}
public:
	bool empty()
	{
		return m_size == 0;
	}
	int size()
	{
		return m_size;
	}
	int capacity()
	{
		return m_capacity;
	}
	int find(T data)
	{
		for (int i = 0; i < m_size; ++i)
			if (m_list[i] == data)
				return i;
		return -1;
	}
	T& operator[](int pos)
	{
		if (pos >= m_size)
		{
			cout << "越界访问！" << endl;
			exit(-1);
		}
		return m_list[pos];
	}
	List& operator=(List& list)
	{
		if (m_capacity > list.m_size) // 如果空间足够就没必要重复开辟
		{
			for (int i = 0; i < list.m_size; ++i)
				m_list[i] = list.m_list[i];
			m_size = list.m_size();
			return *this;
		}
		m_capacity = list.m_size + perMore;
		T* newSpace = new T[m_capacity];
		for (int i = 0; i < list.m_size; ++i)
			m_list[i] = list.m_list[i];
		m_size = list.m_size();
		delete[] m_list;
		m_list = newSpace;
		return *this;
	}
private:
	void moreCapacity()
	{
		T* newSpace = new T[m_capacity + perMore];
		if (newSpace == m_list) // 如果是直接在后面开辟就不用重复拷贝
		{
			m_capacity += perMore;
			return;
		}
		for (int i = 0; i < m_capacity; ++i)
			newSpace[i] = m_list[i];
		m_capacity += perMore;
		delete[] m_list;
		m_list = newSpace;
	}
};
int main()
{
	List<int> list;
	list.push_back(1);
	list.push_back(2);
	list.push_back(3);
	list.push_back(4);
	list.push_back(5);
	for (int i = 0; i < list.size(); ++i)
	{
		cout << list[i] << " ";
	}
	return 0;
}